package com.esri.core.tasks.geodatabase;

import com.esri.core.ags.FeatureServiceInfo;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.MultiPoint;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.f;
import com.esri.core.internal.util.d;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class GenerateGeodatabaseParameters extends f {
    private static final String ASYNC_FIELD = "async";
    private static final String CLIENT_TIME_FIELD = "clientTime";
    private static final String DATA_FORMAT_FIELD = "dataFormat";
    private static final String GEOMETRY_FIELD = "geometry";
    private static final String GEOMETRY_TYPE_FIELD = "geometryType";
    private static final String IN_SR_FIELD = "inSR";
    private static final String LAYERS_FIELD = "layers";
    private static final String LAYER_QUERIES_FIELD = "layerQueries";
    private static final String REPLICA_NAME_FIELD = "replicaName";
    private static final String REPLICA_OPTIONS_FIELD = "replicaOptions";
    private static final String REPLICA_SR_FIELD = "replicaSR";
    private static final String RETURN_ATTACHMENTS_DATA_BY_URL_FIELD = "returnAttachmentDatabyURL";
    private static final String RETURN_ATTACHMENTS_FIELD = "returnAttachments";
    private static final String SYNC_MODEL_FIELD = "syncModel";
    private static final String TRANSPORT_TYPE_FIELD = "transportType";
    private static final long serialVersionUID = 1;
    private SpatialReference _inSpatialRef;
    private boolean _isForRegisterGeodatabase;
    private int[] _layerIds;
    private SpatialReference _outSpatialRef;
    private LayerQueries _queries;
    private Geometry _replicaExtent;
    private String _replicaName;
    private String _replicaOptions;
    private boolean _returnAttachments;
    private SyncModel _syncModel;

    public GenerateGeodatabaseParameters(FeatureServiceInfo featureServiceInfo) {
        this(featureServiceInfo, featureServiceInfo.getInitialExtent(), featureServiceInfo.getSpatialReference());
    }

    public GenerateGeodatabaseParameters(FeatureServiceInfo featureServiceInfo, Geometry geometry, SpatialReference spatialReference) {
        this(featureServiceInfo, geometry, spatialReference, (LayerQueries) null, false);
    }

    public GenerateGeodatabaseParameters(FeatureServiceInfo featureServiceInfo, Geometry geometry, SpatialReference spatialReference, LayerQueries layerQueries, boolean z) {
        this(featureServiceInfo.getLayerIds(), layerQueries, geometry, spatialReference, z, determineSyncModel(featureServiceInfo), null);
    }

    public GenerateGeodatabaseParameters(int[] iArr, LayerQueries layerQueries, Geometry geometry, SpatialReference spatialReference, boolean z, SyncModel syncModel, SpatialReference spatialReference2) {
        this._syncModel = SyncModel.GEODATABASE;
        this._isForRegisterGeodatabase = false;
        this._layerIds = iArr;
        this._queries = layerQueries;
        this._replicaExtent = geometry;
        this._inSpatialRef = spatialReference;
        this._outSpatialRef = spatialReference2;
        this._returnAttachments = z;
        this._syncModel = syncModel;
    }

    public GenerateGeodatabaseParameters(String str) throws IOException {
        this._syncModel = SyncModel.GEODATABASE;
        this._isForRegisterGeodatabase = false;
        this._replicaOptions = "{\"registerExistingData\":" + str + "}";
        this._isForRegisterGeodatabase = true;
    }

    public GenerateGeodatabaseParameters(int[] iArr, Geometry geometry, SpatialReference spatialReference, boolean z, SyncModel syncModel) {
        this(iArr, null, geometry, spatialReference, z, syncModel, null);
    }

    public GenerateGeodatabaseParameters(int[] iArr, Geometry geometry, SpatialReference spatialReference, boolean z, SyncModel syncModel, SpatialReference spatialReference2) {
        this(iArr, null, geometry, spatialReference, z, syncModel, spatialReference2);
    }

    private GenerateGeodatabaseParameters() {
        this._syncModel = SyncModel.GEODATABASE;
        this._isForRegisterGeodatabase = false;
    }

    @Override // com.esri.core.internal.tasks.f
    public Map<String, String> generateRequestParams() throws Exception {
        HashMap hashMap = new HashMap();
        hashMap.put("f", "json");
        String str = this._replicaName;
        if (str != null && str.length() > 0) {
            hashMap.put(REPLICA_NAME_FIELD, this._replicaName);
        }
        if (!this._isForRegisterGeodatabase) {
            int[] iArr = this._layerIds;
            if (iArr != null) {
                hashMap.put(LAYERS_FIELD, d.b(iArr));
            }
            LayerQueries layerQueries = this._queries;
            if (layerQueries != null && layerQueries.size() > 0) {
                hashMap.put(LAYER_QUERIES_FIELD, this._queries.toJson());
            }
            Geometry geometry = this._replicaExtent;
            if (geometry != null) {
                hashMap.put(GEOMETRY_FIELD, GeometryEngine.geometryToJson(null, geometry));
            }
            hashMap.put(GEOMETRY_TYPE_FIELD, d.a(this._replicaExtent.getType()));
            if (this._inSpatialRef != null) {
                hashMap.put(IN_SR_FIELD, new Integer(this._inSpatialRef.getID()).toString());
            }
            if (this._outSpatialRef != null) {
                hashMap.put(REPLICA_SR_FIELD, new Integer(this._outSpatialRef.getID()).toString());
            }
            hashMap.put(RETURN_ATTACHMENTS_FIELD, this._returnAttachments ? "true" : "false");
            hashMap.put(SYNC_MODEL_FIELD, this._syncModel.getValue());
            hashMap.put(DATA_FORMAT_FIELD, "sqlite");
        } else {
            hashMap.put(REPLICA_OPTIONS_FIELD, this._replicaOptions);
            hashMap.put(DATA_FORMAT_FIELD, "json");
        }
        hashMap.put(TRANSPORT_TYPE_FIELD, "esriTransportTypeUrl");
        hashMap.put(ASYNC_FIELD, "true");
        hashMap.put(RETURN_ATTACHMENTS_DATA_BY_URL_FIELD, "false");
        hashMap.put(CLIENT_TIME_FIELD, new Long(System.currentTimeMillis()).toString());
        return hashMap;
    }

    @Override // com.esri.core.internal.tasks.f
    public boolean validate() {
        int[] iArr;
        if (!this._isForRegisterGeodatabase) {
            return (this._replicaExtent == null || (iArr = this._layerIds) == null || iArr.length <= 0) ? false : true;
        }
        String str = this._replicaOptions;
        return str != null && str.length() > 0;
    }

    public int[] getLayerIds() {
        return this._layerIds;
    }

    public void setLayerIds(int[] iArr) {
        if (iArr == null || iArr.length < 1) {
            throw new IllegalArgumentException("Layer IDs should contain at least one layer");
        }
        this._layerIds = iArr;
    }

    public LayerQueries getQueries() {
        return this._queries;
    }

    public void setQueries(LayerQueries layerQueries) {
        if (layerQueries == null) {
            throw new IllegalArgumentException("Layer queries must be non-null");
        }
        this._queries = layerQueries;
    }

    public Geometry getExtent() {
        return this._replicaExtent;
    }

    public void setExtent(Geometry geometry) {
        if (geometry == null) {
            throw new IllegalArgumentException("Geodatabase data extent must be non-null");
        }
        this._replicaExtent = geometry;
    }

    public SyncModel getSyncModel() {
        return this._syncModel;
    }

    public void setSyncModel(SyncModel syncModel) {
        if (syncModel == null) {
            throw new IllegalArgumentException("Sync model must be either LAYER or REPLICA");
        }
        this._syncModel = syncModel;
    }

    public SpatialReference getInSpatialRef() {
        return this._inSpatialRef;
    }

    public void setInSpatialRef(SpatialReference spatialReference) {
        if (spatialReference == null) {
            throw new IllegalArgumentException("Spatial reference for geodatabase extent must be non-null");
        }
        this._inSpatialRef = spatialReference;
    }

    public SpatialReference getOutSpatialRef() {
        return this._outSpatialRef;
    }

    public void setOutSpatialRef(SpatialReference spatialReference) {
        if (spatialReference == null) {
            throw new IllegalArgumentException("Spatial reference for geodatabase extent must be non-null");
        }
        this._outSpatialRef = spatialReference;
    }

    public boolean isReturnAttachments() {
        return this._returnAttachments;
    }

    public void setReturnAttachments(boolean z) {
        this._returnAttachments = z;
    }

    public String toJson() throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        String str = this._replicaName;
        if (str != null && str.length() > 0) {
            a.writeStringField(REPLICA_NAME_FIELD, this._replicaName);
        }
        if (!this._isForRegisterGeodatabase) {
            a.writeFieldName(LAYERS_FIELD);
            a.writeRawValue(d.b(this._layerIds));
            LayerQueries layerQueries = this._queries;
            if (layerQueries != null && layerQueries.size() > 0) {
                a.writeStringField(LAYER_QUERIES_FIELD, this._queries.toJson());
            }
            a.writeFieldName(GEOMETRY_FIELD);
            a.writeRawValue(GeometryEngine.geometryToJson(this._inSpatialRef, this._replicaExtent));
            SpatialReference spatialReference = this._outSpatialRef;
            if (spatialReference != null) {
                a.writeNumberField(REPLICA_SR_FIELD, spatialReference.getID());
            }
            a.writeBooleanField(RETURN_ATTACHMENTS_FIELD, this._returnAttachments);
            a.writeStringField(SYNC_MODEL_FIELD, this._syncModel.getValue());
        } else {
            a.writeStringField(REPLICA_OPTIONS_FIELD, this._replicaOptions);
        }
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    public static GenerateGeodatabaseParameters fromJson(JsonParser jsonParser) throws Exception {
        if (d.c(jsonParser)) {
            GenerateGeodatabaseParameters generateGeodatabaseParameters = new GenerateGeodatabaseParameters();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if (currentName.equals(REPLICA_NAME_FIELD)) {
                    generateGeodatabaseParameters._replicaName = jsonParser.getText();
                } else if (currentName.equals(REPLICA_OPTIONS_FIELD)) {
                    generateGeodatabaseParameters._replicaOptions = jsonParser.getText();
                    generateGeodatabaseParameters._isForRegisterGeodatabase = true;
                } else if (currentName.equals(LAYERS_FIELD)) {
                    ArrayList arrayList = new ArrayList();
                    jsonParser.nextToken();
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        arrayList.add(Integer.valueOf(jsonParser.getIntValue()));
                    }
                    generateGeodatabaseParameters._layerIds = new int[arrayList.size()];
                    for (int i = 0; i < arrayList.size(); i++) {
                        generateGeodatabaseParameters._layerIds[i] = ((Integer) arrayList.get(i)).intValue();
                    }
                } else if (currentName.equals(LAYER_QUERIES_FIELD)) {
                    generateGeodatabaseParameters._queries = LayerQueries.fromJson(jsonParser);
                } else if (currentName.equals(GEOMETRY_FIELD)) {
                    MapGeometry jsonToGeometry = GeometryEngine.jsonToGeometry(jsonParser);
                    generateGeodatabaseParameters._replicaExtent = jsonToGeometry.getGeometry();
                    generateGeodatabaseParameters._inSpatialRef = jsonToGeometry.getSpatialReference();
                } else if (currentName.equals(REPLICA_SR_FIELD)) {
                    generateGeodatabaseParameters._outSpatialRef = SpatialReference.create(jsonParser.getIntValue());
                } else if (currentName.equals(RETURN_ATTACHMENTS_FIELD)) {
                    generateGeodatabaseParameters._returnAttachments = jsonParser.getBooleanValue();
                } else if (currentName.equals(SYNC_MODEL_FIELD)) {
                    generateGeodatabaseParameters._syncModel = SyncModel.fromString(jsonParser.getText());
                } else {
                    jsonParser.skipChildren();
                }
            }
            return generateGeodatabaseParameters;
        }
        return null;
    }

    public int hashCode() {
        SpatialReference spatialReference = this._inSpatialRef;
        int id = ((spatialReference == null ? 0 : spatialReference.getID()) + 31) * 31;
        SpatialReference spatialReference2 = this._outSpatialRef;
        int id2 = (((((id + (spatialReference2 == null ? 0 : spatialReference2.getID())) * 31) + (this._isForRegisterGeodatabase ? 1231 : 1237)) * 31) + Arrays.hashCode(this._layerIds)) * 31;
        LayerQueries layerQueries = this._queries;
        int hashCode = (id2 + (layerQueries == null ? 0 : Arrays.hashCode(layerQueries.toArray()))) * 31;
        Geometry geometry = this._replicaExtent;
        int generateGeometryHashcode = (hashCode + (geometry == null ? 0 : generateGeometryHashcode(geometry))) * 31;
        String str = this._replicaName;
        int hashCode2 = (generateGeometryHashcode + (str == null ? 0 : str.hashCode())) * 31;
        String str2 = this._replicaOptions;
        int hashCode3 = (((hashCode2 + (str2 == null ? 0 : str2.hashCode())) * 31) + (this._returnAttachments ? 1231 : 1237)) * 31;
        SyncModel syncModel = this._syncModel;
        return hashCode3 + (syncModel != null ? syncModel.toString().hashCode() : 0);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            GenerateGeodatabaseParameters generateGeodatabaseParameters = (GenerateGeodatabaseParameters) obj;
            SpatialReference spatialReference = this._inSpatialRef;
            if (spatialReference == null) {
                if (generateGeodatabaseParameters._inSpatialRef != null) {
                    return false;
                }
            } else if (spatialReference.getID() != generateGeodatabaseParameters._inSpatialRef.getID()) {
                return false;
            }
            SpatialReference spatialReference2 = this._outSpatialRef;
            if (spatialReference2 == null) {
                if (generateGeodatabaseParameters._outSpatialRef != null) {
                    return false;
                }
            } else if (spatialReference2.getID() != generateGeodatabaseParameters._outSpatialRef.getID()) {
                return false;
            }
            if (this._isForRegisterGeodatabase == generateGeodatabaseParameters._isForRegisterGeodatabase && Arrays.equals(this._layerIds, generateGeodatabaseParameters._layerIds)) {
                LayerQueries layerQueries = this._queries;
                if (layerQueries == null) {
                    if (generateGeodatabaseParameters._queries != null) {
                        return false;
                    }
                } else if (!layerQueries.equals(generateGeodatabaseParameters._queries)) {
                    return false;
                }
                Geometry geometry = this._replicaExtent;
                if (geometry == null) {
                    if (generateGeodatabaseParameters._replicaExtent != null) {
                        return false;
                    }
                } else if (!GeometryEngine.equals(geometry, generateGeodatabaseParameters._replicaExtent, this._inSpatialRef)) {
                    return false;
                }
                String str = this._replicaName;
                if (str == null) {
                    if (generateGeodatabaseParameters._replicaName != null) {
                        return false;
                    }
                } else if (!str.equals(generateGeodatabaseParameters._replicaName)) {
                    return false;
                }
                String str2 = this._replicaOptions;
                if (str2 == null) {
                    if (generateGeodatabaseParameters._replicaOptions != null) {
                        return false;
                    }
                } else if (!str2.equals(generateGeodatabaseParameters._replicaOptions)) {
                    return false;
                }
                return this._returnAttachments == generateGeodatabaseParameters._returnAttachments && this._syncModel == generateGeodatabaseParameters._syncModel;
            }
            return false;
        }
        return false;
    }

    private static SyncModel determineSyncModel(FeatureServiceInfo featureServiceInfo) {
        return featureServiceInfo.getSyncCapabilities().isPerLayerSyncSupported() ? SyncModel.LAYER : SyncModel.GEODATABASE;
    }

    private int generateGeometryHashcode(Geometry geometry) {
        int i = 1;
        if (geometry != null) {
            int i2 = 0;
            if (geometry instanceof MultiPath) {
                MultiPath multiPath = (MultiPath) geometry;
                while (i2 < multiPath.getPointCount()) {
                    i = generatePointHashCode(31, i, multiPath.getPoint(i2));
                    i2++;
                }
                return i;
            } else if (geometry instanceof MultiPoint) {
                MultiPoint multiPoint = (MultiPoint) geometry;
                while (i2 < multiPoint.getPointCount()) {
                    i = generatePointHashCode(31, i, multiPoint.getPoint(i2));
                    i2++;
                }
                return i;
            } else if (geometry instanceof Envelope) {
                Envelope envelope = (Envelope) geometry;
                return generatePointHashCode(31, generatePointHashCode(31, generatePointHashCode(31, generatePointHashCode(31, 1, envelope.getUpperLeft()), envelope.getUpperRight()), envelope.getLowerLeft()), envelope.getLowerRight());
            } else {
                return generatePointHashCode(31, 1, (Point) geometry);
            }
        }
        return 1;
    }

    private int generatePointHashCode(int i, int i2, Point point) {
        return (i * i2) + Double.valueOf(point.getX()).hashCode() + Double.valueOf(point.getY()).hashCode();
    }
}
